/**
 * 使用Dismissible组件实现滑动删除
 * 它是根据Key来删除ListView中的某一项的
 * 请注意ListView.builder中itemBuilder: (context, index)传进的index
 * 他不是list中的下标，而是这个组件在当前屏幕上所占的位置
 * 
 * https://github.com/OpenFlutter/Flutter-Notebook/blob/master/mecury_project/example/swipe_to_dismiss/lib/swipe_to_dismiss_demo.dart
 */
import 'package:flutter/material.dart';

class SlideDeleteItemPage extends StatelessWidget {
  /// 使用 List.generate进行初始化集合
  final List<String> listData =
      List.generate(20, (index) => "This is number $index");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("SlideDeleteItemPage")),
      body: Column(
        children: <Widget>[_itemSingleWidget(), _itemListView(context)],
      ),
    );
  }

  Widget _itemSingleWidget() {
    return Dismissible(
      key: Key("_itemSingleWidget"),
      direction: DismissDirection.horizontal,
      background: Container(
        color: Colors.pinkAccent,
        alignment: Alignment.center,
        child: Text(
          "删除",
          style: TextStyle(color: Colors.white),
        ),
      ),
      child: Container(
        height: 70,
        color: Colors.blueAccent,
        alignment: Alignment.center,
        child: Text(
          "滑动移除我",
          style: TextStyle(color: Colors.white, fontSize: 18),
        ),
      ),
    );
  }

  Widget _itemListView(BuildContext context) {
    return Expanded(
        child: ListView.builder(
            itemCount: listData.length,
            itemBuilder: (context, index) {
              return Dismissible(
                key: Key(listData[index]),
                background: Container(
                  color: Colors.blueAccent,
                ),
                child: ListTile(
                  title: Text("item:${listData[index]}"),
                ),
                onDismissed: (direction) {
                  /// do something
                },
              );
            }));
  }
}
